<?php
declare(strict_types=1);

namespace App\AdminApi\Module\Admin\Api;

use App\AdminApi\AdminApiBasicsController;
use App\AdminApi\Module\Admin\Service\AdministerService;
use App\Sys;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Psr\Http\Message\ResponseInterface;
use Zms\Guard\Auth;
use Zms\HyperfResponse\Response;

#[
    Auth(guard: self::GUARD, label: ['后台管理'], app: self::APP),
    Controller(prefix: "/admin-api/admin", server: self::SERVER)
]
class SettingsController extends AdminApiBasicsController
{
    #[Inject]
    protected AdministerService $service;

    #[
        GetMapping('settings'),
        Auth(label: ['后台设置'], sign: 'admin.settings', view: '/admin/settings/index')
    ]
    public function getSettings(): ResponseInterface
    {
        return Response::Api()->success(
            data: $this->service->getConfigureForm('admin', $this->request->getPathInfo(), '后台配置修改', '150px')
        );
    }

    #[
        PostMapping('settings', ['POST', 'GET']),
        Auth(label: ['后台设置'], relyon: ['admin.settings'])
    ]
    public function setSettings(): ResponseInterface
    {
        $this->service->setConfigure('admin', $this->request->post());
        Sys::cache()->clear();
        return Response::Api()->success("设置成功");
    }
}